import 'dart:async';

import 'package:flutter/material.dart';

class CustomBanner<T> extends StatefulWidget {
  final List<T> images;
  final double height;
  final Function onTouch;
  final bool autoScroll;
  final int duration;

  CustomBanner(this.images,
      {this.height = 200,
      this.onTouch,
      this.autoScroll = true,
      this.duration = 6})
      : assert(images != null);

  @override
  _CustomBannerState createState() => _CustomBannerState();
}

class _CustomBannerState extends State<CustomBanner> {
  Timer _timer;
  int _currentIndex;

  PageController _pageController;

  @override
  void initState() {
    _currentIndex = 0;
    if (widget.autoScroll) {
      _timer =
          Timer.periodic(Duration(seconds: widget.duration), _timerCallback);
    }

    _pageController = new PageController();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      child: Stack(
        children: <Widget>[_buildImages(), _buildIndicator(),
        //  _buildTip()
         ],
      ),
    );
  }

  Widget _buildImages() {
    return PageView.builder(
      controller: _pageController,
      onPageChanged: _pageChange,
      itemBuilder: (context, index) {
        return InkWell(
            onTap: () {
              _onPageClick(index);
            },
            child: widget.images.isNotEmpty
                ? Image.network(
                    widget.images[index].imagePath,
                    fit: BoxFit.fill,
                  )
                : null);
      },
      itemCount: widget.images.length,
    );
  }

  Widget _buildIndicator() {
    var length = widget.images.length;
    return Positioned(
      right: 20,
      child: Row(
        children: widget.images.map((s) {
          return Padding(
              padding: EdgeInsets.symmetric(horizontal: 3.0),
              child: ClipOval(
                child: Container(
                  width: 8,
                  height: 8,
                  color: s == widget.images[_currentIndex % length]
                      ? Colors.white
                      : Colors.grey,
                ),
              ));
        }).toList(),
      ),
      bottom: 10,
    );
  }

  Widget _buildTip() {
    return Positioned(
        left: 20,
        bottom: 5,
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 3.0),
          child: Text(
            '${widget.images[_currentIndex].title}',
            style: TextStyle(color: Colors.red),
          ),
        ));
  }

  _timerCallback(Timer timer) {
    if (widget.images.length > 1) {
      _currentIndex++;

      _pageController.animateToPage(_currentIndex % widget.images.length,
          duration: const Duration(microseconds: 200),
          curve: Curves.fastOutSlowIn);
    }
  }

  _onPageClick(int index) {
    if (widget.onTouch != null) {
      widget.onTouch(index);
    }
  }

  _pageChange(int index) {
    if (widget.images.length == 1) {
      return;
    }
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  void dispose() {
    _timer?.cancel();
    _pageController?.dispose();
    super.dispose();
  }
}
